<?php
require_once 'classes/Database.php';
require_once 'classes/ProductManager.php';
require_once 'classes/SettingsHelper.php';

$productManager = new ProductManager();
$products = $productManager->getAllProducts();
$siteName = getSiteName();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($siteName); ?> - 商品列表</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .product-card {
            transition: transform 0.3s, box-shadow 0.3s;
            margin-bottom: 20px;
        }
        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0,0,0,0.15);
        }
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 60px 0;
            margin-bottom: 40px;
        }
        .price {
            color: #e74c3c;
            font-size: 1.5rem;
            font-weight: bold;
        }
        .stock {
            color: #27ae60;
        }
        .out-of-stock {
            color: #95a5a6;
        }
        .btn-buy {
            background: linear-gradient(45deg, #667eea, #764ba2);
            border: none;
            color: white;
        }
        .btn-buy:hover {
            background: linear-gradient(45deg, #764ba2, #667eea);
            color: white;
        }
    </style>
</head>
<body>
    <!-- 头部 -->
    <div class="header text-center">
        <div class="container">
            <h1 class="display-4 mb-3">
                <i class="fas fa-shopping-cart me-3"></i><?php echo htmlspecialchars($siteName); ?>
            </h1>
            <p class="lead">安全便捷的虚拟商品购买平台</p>
        </div>
    </div>

    <!-- 商品列表 -->
    <div class="container">
        <div class="row mb-4">
            <div class="col-12">
                <h2 class="text-center mb-4">
                    <i class="fas fa-gift me-2"></i>热门商品
                </h2>
            </div>
        </div>

        <div class="row">
            <?php if (empty($products)): ?>
                <div class="col-12 text-center">
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>暂无商品上架
                    </div>
                </div>
            <?php else: ?>
                <?php foreach ($products as $product): ?>
                    <div class="col-md-4 col-sm-6">
                        <div class="card product-card h-100">
                            <?php if (!empty($product['image'])): ?>
                                <img src="<?php echo htmlspecialchars($product['image']); ?>" class="card-img-top" alt="<?php echo htmlspecialchars($product['name']); ?>" style="height: 200px; object-fit: cover;">
                            <?php else: ?>
                                <div class="card-img-top bg-light d-flex align-items-center justify-content-center" style="height: 200px;">
                                    <i class="fas fa-box-open fa-3x text-muted"></i>
                                </div>
                            <?php endif; ?>
                            
                            <div class="card-body d-flex flex-column">
                                <h5 class="card-title"><?php echo htmlspecialchars($product['name']); ?></h5>
                                <p class="card-text flex-grow-1"><?php echo htmlspecialchars($product['description']); ?></p>
                                
                                <div class="mt-auto">
                                    <div class="d-flex justify-content-between align-items-center mb-3">
                                        <span class="price">¥<?php echo number_format($product['price'], 2); ?></span>
                                        <span class="stock <?php echo $product['stock'] <= 0 ? 'out-of-stock' : ''; ?>">
                                            <i class="fas fa-box me-1"></i>
                                            <?php echo $product['stock'] > 0 ? "库存: {$product['stock']}" : "缺货"; ?>
                                        </span>
                                    </div>
                                    
                                    <?php if ($product['stock'] > 0): ?>
                                        <a href="buy.php?id=<?php echo $product['id']; ?>" class="btn btn-buy w-100">
                                            <i class="fas fa-shopping-cart me-2"></i>立即购买
                                        </a>
                                    <?php else: ?>
                                        <button class="btn btn-secondary w-100" disabled>
                                            <i class="fas fa-times-circle me-2"></i>已售罄
                                        </button>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <!-- 页脚 -->
    <footer class="bg-dark text-light mt-5 py-4">
        <div class="container text-center">
            <p class="mb-0">&copy; 2024 <?php echo htmlspecialchars($siteName); ?>. 保留所有权利.</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
